package com.example.g32.notice

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.entity.NoticeEntityItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.UiState
import com.example.g32.R
import com.example.g32.databinding.ActivityNoticeBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/app/NoticeActivity")
class NoticeActivity : BaseActivity<ActivityNoticeBinding, NoticeIntent, NoticeViewModel>() {
    override val binding by lazy {
        ActivityNoticeBinding.inflate(layoutInflater)
    }

    override val vm by viewModels<NoticeViewModel>()

    val noticeAdapter by lazy { NoticeAdapter() }

    override fun initView() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        Log.i("xs", it.data.toString())
                        val items = it.data as Collection<NoticeEntityItem>
                        noticeAdapter.addData(items.filter { !it.title.isNullOrEmpty() && !it.content.isNullOrEmpty() })
                        noticeAdapter.notifyDataSetChanged()
                    }
                    is UiState.onError->{
                        Log.d("xs", it.msg)
                    }
                    else->{}
                }
            }
        }
    }

    override fun initData() {
        binding.rvNotice.apply {
            adapter = noticeAdapter
            layoutManager = LinearLayoutManager(this@NoticeActivity)
        }
        vm.sendIntent(NoticeIntent.getNotice(1,10))
    }


}